<!--
 * @Author: White
 * @description:最新音乐
 * @LastEditTime: 2021-08-26 16:55:50
-->

<template>
  <div class="new-music" :style="{ 'color': theme.text }">
    <div class="music-title">
      <h3>最新音乐</h3>
      <i class="el-icon-arrow-right"></i>
    </div>
    <div class="music-content">
      <div class="music-item" v-for="(item, index) in list" :key="index" @click="playSongById(item.id)">
        <div class="pic">
          <div class="yuan"></div>
          <span class="triangle"></span>
          <img :src="item.picUrl" alt="" />
        </div>
        <div class="music-text">
            <span>{{ index + 1}}</span>
            <p class="title">{{ item.name }}</p>
            <p class="desc">{{ item.song.artists[0].name }}</p>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { useStore } from 'vuex'
import { reactive, onMounted, computed, toRefs } from 'vue'
import { getNewMusic, getMusicDetail } from '@/api/music'

export default {
  setup () {
    const store = useStore()
    const theme = computed(() => store.state.theme)
    const state = reactive({
      list: []
    })

    onMounted(async () => {
      getList()
    })

    // 播放歌曲
    const playSongById = async (id) => {
      let parmas = { ids: id }
      const res = await getMusicDetail(parmas)
      store.commit("setCurrentSong", res.data)
    }

    async function getList() {
      const res = await getNewMusic()
      if (res.data.code === 200) {
        state.list = res.data.result.splice(0, 10)
      }
    }

    return {
      ...toRefs(state),
      theme,
      playSongById
    }
  }
}
</script>

<style lang="scss" scoped>
.new-music {
  .music-title {
    height: 30px;
    display: flex;
    align-items: center;
    h3 {
      line-height: 30px;
      font-size: 16px;
      font-weight: 700;
      text-decoration: none;
    }
  }
  .music-content {
    display: flex;
    justify-content: space-between;
    flex-wrap: wrap;
    .music-item {
      width: 46%;
      display: flex;
      align-items: center;
      padding: 10px 0px;
      border-bottom: 1px solid #ededed;
      cursor: pointer;
      .pic {
        position: relative;
        .yuan {
          width: 26px;
          height: 26px;
          background-color: #fff;
          position: absolute;
          border-radius: 50%;
          top: 17px;
          left: 17px;
          opacity: .7;
        }
        .triangle {
          width:10px;
          height:0px;
          position: absolute;
          top: 20px;
          left: 26px;
          border-top: 10px solid transparent;
          border-bottom: 10px solid transparent;
          border-left: 10px solid red;
        }
        img {
          width: 60px;
          height: 60px;
          border-radius: 10px;
          margin-right: 40px;
        }
      }
      .music-text {
          position: relative;
          span {
            text-align: center;
            font-size: 12px;
            color: #b7b7b7;
            position: absolute;
            top: 10px;
            left: -20px;
          }
          .title {
            font-size: 14px;
            font-weight: 700;
            margin-bottom: 10px;
            display: flex;
            align-items: center;
            overflow: hidden;
            white-space: nowrap;
            text-overflow: ellipsis;
            word-break: break-all;
          }
          .desc {
            font-size: 13px;
            color: #666;
          }
      }
    }
  }
}
</style>
